package com.questionnaires.model;

import org.hibernate.validator.constraints.NotBlank;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * The Question entity class
 *
 * @author adriana.calina.oros@gmail.com
 */

@Entity
@Table(name = "QUESTIONS")
public class Question implements Serializable {

    private long id;
//    private Questionnaire questionnaire;
    @NotBlank
    private String text;
    private QuestionResponseType responseType;
    private List<Response> responses;

    public Question() {
    }

    @Id
    @GeneratedValue
    @Column(name = "ID")
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column(name = "text", nullable = false)
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Enumerated(EnumType.STRING)
    public QuestionResponseType getResponseType() {
        return responseType;
    }

    public void setResponseType(QuestionResponseType responseType) {
        this.responseType = responseType;
    }

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "question_id", nullable = false)
    public List<Response> getResponses() {
        return responses;
    }

    public void setResponses(List<Response> responses) {
        this.responses = responses;
    }

//    @ManyToOne
//    @JoinColumn(name = "questionnaire_ID", nullable = false, insertable = false, updatable = false)
//    public Questionnaire getQuestionnaire() {
//        return questionnaire;
//    }
//
//
//    public void setQuestionnaire(Questionnaire questionnaire) {
//        this.questionnaire = questionnaire;
//    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Question question = (Question) o;

        if (id != question.id) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return (int) (id ^ (id >>> 32));
    }

    @Override
    public String toString() {
        return "Question{" +
                "text='" + text + '\'' +
                '}';
    }
}
